package wine.actions;

import com.opensymphony.xwork2.Action;

import wine.logic.entity.WUsrInf;
import wine.web.Struts2Support;
import wine.ejb.utilities.Constants;
import wine.ejb.utilities.Utility;
import wine.ejb.utilities.Log;

public class WUsrInfAction extends Struts2Support<WUsrInf> {

	private static final long serialVersionUID = 1L;

	private String captchaImgId;
	private String captCha;
	
	public String login(){
		Log.debug("------"+Utility.getCurrentDate()+"some want to login");
		if (obj==null||obj.getLoginCode()==null||obj.getLoginCode().trim().equals("")||obj.getPasswd()==null||obj.getPasswd().trim().equals("")) {
			addActionError("用户名或密码错误！！！");
			Log.error("---------------"+Utility.getCurrentDate()+" login failed");
			return Action.INPUT;
		}
		obj=getWUsrInfDao().validLogin(obj.getLoginCode(), obj.getPasswd());
		if(obj!=null&&obj.getId()>0){
			request.getSession().setAttribute(Constants.LOGIN_USER_INFO, obj);
			Log.debug("======================"+obj.getLoginCode()+"==========login successful");
			return Action.SUCCESS;
		}
		return Action.INPUT;
	}
	
	
	/**
	 * 检查验证码是否正确
	 * 
	 * @param captCha
	 *            输入的验证码
	 * @param captchaImgId
	 *            页面自动生成的验证码图片的id
	 * @return true 验证码正确,false 验证码错误
	 */
	public String isRightCaptCha() {
		String imageCode = (String) readSession(captchaImgId);
		if (Utility.isNotEmpty(imageCode)) {
			if (imageCode.equals(captCha)) {
				jsonMap.put(Constants.RETURN_TAG, Boolean.TRUE);
			} else {
				jsonMap.put(Constants.RETURN_MESSAGE, Constants.ERROR_CAPTCHA);
				jsonMap.put(Constants.RETURN_TAG, Boolean.FALSE);
			}
		} else {
			jsonMap.put(Constants.RETURN_MESSAGE, Constants.TIME_OUT_CAPTCHA);
			jsonMap.put(Constants.RETURN_TAG, Boolean.FALSE);
		}
		return Action.SUCCESS;
	}

	public String getCaptchaImgId() {
		return captchaImgId;
	}

	public void setCaptchaImgId(String captchaImgId) {
		this.captchaImgId = captchaImgId;
	}

	public String getCaptCha() {
		return captCha;
	}

	public void setCaptCha(String captCha) {
		this.captCha = captCha;
	}
	
}
